package com.koron.tool.frame.bean;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.koron.util.Util;

public class SelectBean {
	private File file;
	private String content;
	private String id;
	private String nameSpace;
	/**返回的种类
	 */
	private String resultType;
	private String parameterType;
	/**
	 * 返回的对象名
	 */
	private String returnType;

	public SelectBean(File file, String id, String content,String nameSpace) {
		this.file = file;
		this.id = id;
		this.nameSpace = nameSpace;
		parameterType = Util.getAttribute(content,"parameterType");
		returnType = Util.getAttribute(content,"resultMap");
		if (returnType!=null) {
			resultType = "returnMap";
		}else
		{
			returnType = Util.getAttribute(content, "resultType");
			resultType = "resultType";
		}
		Pattern pattern = Pattern.compile("<select[^>]*>([\\d\\D]*?)</select>");
		Matcher m = pattern.matcher(content);
		if (m.find())
			this.content = m.group(1);
	}

	/**
	 * 把内容写入XML文件中
	 * 
	 * @return 写入是否成功
	 * @throws IOException
	 */
	public boolean saveToXml(String oId) throws IOException {
		String str = "<select [^>]*id=\\\"" + oId + "\\\"[^>]*>([\\d\\D]*?)</select>";
		String encode = Util.getXmlFileEncode(file);
		String fileStr = Util.fileToString(file, encode);
		Matcher m = Pattern.compile(str).matcher(fileStr);
		if (m.find()) {
			StringBuilder sb = new StringBuilder(fileStr);
			// 找出的select标签里的字符串
			StringBuilder context = new StringBuilder(m.group());
			context.replace(m.start(1) - m.start(), m.end(1) - m.start(), content);
			CharSequence cont = Util.setAttribute(context, "parameterType", parameterType);
			cont = Util.setAttribute(cont, "id", id);
			if ("resultType".equals(resultType)) {
				cont = Util.setAttribute(cont, "resultType", returnType);
				cont = Util.setAttribute(cont, "resultMap", "");
			} else {
				cont = Util.setAttribute(cont, "resultMap", returnType);
				cont = Util.setAttribute(cont, "resultType", "");
			}

			sb.replace(m.start(), m.end(), cont.toString());
			PrintWriter pw = new PrintWriter(file, encode);
			pw.print(sb.toString());
			pw.close();
			return true;
		}
		return false;
	}


	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getResultType() {
		return resultType;
	}

	public void setResultType(String resultType) {
		this.resultType = resultType;
	}

	public String getParameterType() {
		return parameterType;
	}

	public void setParameterType(String parameterType) {
		this.parameterType = parameterType;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public String getNameSpace() {
		return nameSpace;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setFile(File file) {
		this.file = file;
	}
	
}